<?php
include_once(APP_PATH."/cartoon/global.php");
class cartoon_model_data extends cartoon_model_bmodel {
	
	private $cache;
	
	public function __construct(){
		parent::__construct();
		
		$this->cache = new cartoon_util_cache();
		
		$this->table = 'cartoon_cartoon';	//漫画表
	}
	
	/**
	 * 添加漫画
	 *
	 * @param unknown_type $para
	 * @return unknown
	 */
	public function add($para){
		return $this->db->insert($this->table,$para);
	}
	
	
	/**
	 * 根据id获取漫画信息
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	public function getRow($id){
		if(is_numeric($id)){
			return $this->db->fetchRow("SELECT * FROM $this->table WHERE id=$id");
		}
		return false;
	}
	
	/**
	 * 缓存获取漫画信息
	 *
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $flush
	 */
	public function getCartoonInfo($cartoon_id,$flush = false){
		if(!is_numeric($cartoon_id)) return false;
		$key = CACHE_PRE_CARTOON.$cartoon_id;
		if(!$info = MemRow::getCache($key) || $flush == true){
			$info = self::getRow($cartoon_id);
			MemRow::setCache($key,$info,0);
		}
		return $info;
	}
	
	
	/**
	 * 更新漫画章节数
	 * @param uint $cartoon_id  漫画id
	 * @param uint $num		更新数量
	 */
	public function upChapterNum($cartoon_id,$num=1){
		if(!is_numeric($cartoon_id)) return false;
		$sql = "UPDATE $this->table SET ct_chapters = ct_chapters+$num,ct_update=".CURRENT_TIME.' WHERE id='.$cartoon_id;
		if($this->db->query($sql)){
			$this->getCartoonInfo($cartoon_id,true);
		}
	}
	
	/**
	 * 设置漫画状态
	 *
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $flag
	 */
	public function recomCartoon($cartoon_id,$flag){
		if(!(is_numeric($cartoon_id) && $flag )) return false;
		$cartoon_id = is_array($cartoon_id)?$cartoon_id:explode(',',$cartoon_id);
		$sql = "UPDATE $this->table SET ct_recoment = $flag WHERE id IN(".implode(',',$cartoon_id).")";
		if($this->db->query($sql)){	
			foreach ($cartoon_id as $v){
				self::getCartoonInfo($v,true);
			}
			return false;
		}
	}
	
	/**
	 * 根据条件，获取漫画列表
	 *
	 * @param unknown_type $para
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getCartoonList($args = array(),$page=1,$pageSize=CT_PAGE_SIZE){
		$sql = "SELECT * FROM $this->table  ";
		$where = ' WHERE 1 ';
		if($args['cartoon_flag']){
			$where.=' AND cartoon_flag = '.$args['cartoon_flag'];
		}
		
		$sql.=$where.' ORDER BY id DESC ';
		
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}	
	
	/**
	 * 获取推荐的漫画类表
	 * @param  int $flag 推荐状态 	
	 * @param  int $num	推荐的数量
	 * @return unknown
	 */
	public function getCartoonListRecom($flag = CONST_CT::RECOM_HOME,$num = 8,$order = ''){
		!$flag && $flag = CONST_CT::RECOM_HOME;
		!$num && $num = 8;
		$sql = " SELECT * FROM $this->table WHERE ct_recoment = $flag  ORDER BY ".($order?$order:' ct_update ').' DESC LIMIT '.$num ;
		return $this->db->fetchAll($sql);		
	}

	
	/**
	 * 根据分类获取tag类表
	 *
	 * @param tinyint $type
	 * @return array();
	 */
	
	public function cacheGetTagByType($type){
		if(!in_array($type,array_keys($_SERVER['cartoon']['tag_type']))) return false;
		
		$rs = $this->cacheGetTags();
		
		return $rs[$type];
	}
	
	
	
	/**
	 * 根据类型获取标签
	 *
	 * @param unknown_type $type
	 * @return unknown
	 */
	
	public function getTagByType($type=NULL){
		$sql = "SELECT * FROM cartoon_tags ";
		if($type && in_array($type,array_keys($_SERVER['cartoon']['tag_type']))){
			$sql.=' WHERE tag_type = '.$type;
		}
		
		$return  = array();
				
		if($rs = $this->db->fetchAll($sql)){
			foreach ($rs as $row){
				$return[$row['tag_type']][] = $row;
			}
		}		
		return $return;
	}
	
	/**
	 * 设置用户阅读漫画记录表
	 *
	 * @param unknown_type $chapter_id
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $uid
	 */
	public function readLog($cartoon_id,$uid,$chapter_id){
		if(!($chapter_id && $cartoon_id && $uid)) return false;
		$sql = "INSERT INTO cartoon_readlog (cartoon_id,uid,chapter_id,read_time) VALUES($cartoon_id,$uid,$chapter_id,".CURRENT_TIME.") ON DUPLICATE KEY UPDATE cartoon_chapter = $chapter_id , read_time =".CURRENT_TIME;
		return $this->db->query($sql);
	}
	
	/**
	 * 添加分类
	 *
	 * @param unknown_type $title
	 * @param unknown_type $order
	 * @return unknown
	 */
	public function addCate($title,$order = 0){
		if(!$title) return false;
		if($this->chkCate($title)) return ERR_EXISTS;
		return $this->db->insert('cartoon_category',array('cate_name'=>$title,'cate_pid'=>0,'cate_order'=>0,'cate_flag'=>1,'cate_time'=>CURRENT_TIME));
	}
	
	/**
	 * 删除分类
	 *
	 * @param unknown_type $cateid
	 * @return unknown
	 */
	public function delCate($cateid){
		if(!$cateid) return false;
		$ids = is_array($cateid)?implode(',',$cateid):$cateid;
		if($this->db->query("DELETE FROM cartoon_category WHERE id IN ($cateid)")){
			//做其他处理
			cartoon_util_interface::cacheGetCateList(true);//更新缓存
			return true;
		}
		return false;
	}
	/**
	 * 检查分类是否已经存在
	 *
	 * @param unknown_type $title
	 * @return unknown
	 */
	public function chkCate($title){
		if(!$title) return false;
		if($rs = $this->db->fetchField("SELECT id FROM cartoon_category WHERE cate_name = '$title'",'id')){
			return true;
		}
		return false;
	}
	/**
	 * 漫画分类
	 *
	 * @return unknown
	 */
	public function getCateList(){
		return $this->db->fetchAll("SELECT * FROM cartoon_category ORDER BY cate_order,id DESC");
	}
	
}
?>